﻿using System;
using System.Collections.Generic;
using province;
using UnityEngine;

namespace Common
{
    public class BorderUtil
    {
        /**
         * 写入顺序：首先写入
         */
        public static byte[] SerializerBorder(List<Border> borders)
        {
            List<Int16> datas = new List<Int16>();
            datas.Add(Convert.ToInt16(borders.Count));
            int ass = 1;
            int i = 0;
            foreach (var border in borders)
            {
                if (border.list.Count == 0)
                {
                    Debug.Log("出错误了！！！！！");
                }
                datas.Add(Convert.ToInt16(border.id));
                ass++;
                datas.Add(Convert.ToInt16(border.list.Count * 2));
                ass++;
                ass += border.list.Count * 2;
                foreach (var point in border.list)
                {
                    datas.Add(Convert.ToInt16(point.x));
                    datas.Add(Convert.ToInt16(point.y));
                }

            }
            Debug.Assert(ass == datas.Count,String.Format("数组大小为：{0}，应该为{1}",datas.Count,ass));
            Byte[] bytes = new byte[datas.Count * 2];
            Int16ToByte(datas.ToArray(), datas.Count,ref bytes);
            return bytes;
        }

        public static List<Border> DeSerializerBorder(Int16[] array)
        {
            var borders = new List<Border>();
            var count = array[0];
            int st = 1;
            try
            {
                for (int i = 0; i < count; ++i)
                {
                    var border = new Border();
                    border.id = array[st++];
                    int pCount = array[st++];
                    for (int j = 0; j < pCount/2; ++j)
                    {
                        int x = array[st++];
                        int y = array[st++];
                        border.list.Add(new Point(x, y));
                    }
                    borders.Add(border);
                }
            }
            catch (Exception e)
            {
                Debug.Log("现在ST的值为：" + st);
            }

            return borders;
        }
        
        public static void Int16ToByte(Int16[] arrInt16, int nInt16Count, ref Byte[] destByteArr)
        {
            //遵守X86规则，低字节放在前面，高字节放在后面
            for (int i = 0; i < nInt16Count; i++ )
            {
                destByteArr[2 * i + 0] = Convert.ToByte((arrInt16[i] & 0x00FF));
                destByteArr[2 * i + 1] = Convert.ToByte((arrInt16[i] & 0xFF00) >> 8);
            }
        }

        public static void ByteToInt16(Byte[] arrByte, int nByteCount, ref Int16[] destInt16Arr)
        {
            int i=0;
            try
            {
                //按两个字节一个整数解析，前一字节当做整数低位，后一字节当做整数高位，调用系统函数转化
                for (i = 0; i < nByteCount / 2; i++)
                {
                    Byte[] tmpBytes = new Byte[2] { arrByte[2 * i + 0], arrByte[2 * i + 1] };
                    destInt16Arr[i] = BitConverter.ToInt16(tmpBytes, 0);
                }
            }
            catch (Exception e)
            {
            }
        }


    }
}